package com.jims.master.modules.patient.health.service;


import com.jims.master.common.persistence.Page;
import com.jims.master.common.service.CrudService;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.modules.patient.health.dao.DailyCheckDao;
import com.jims.master.modules.patient.health.entity.DailyCheck;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 日常检测Service
 * @author xueyx
 * @version 2017-07-14
 */
@Service
@Transactional(readOnly = true)
public class DailyCheckService extends CrudService<DailyCheckDao, DailyCheck> {

	@Autowired
	DailyCheckDao dailyCheckDao;
	@Override
	public Page<DailyCheck> findPage(Page<DailyCheck> page, DailyCheck dailyCheck) {
		return super.findPage(page, dailyCheck);
	}

	public String getRecords(String baseInfo) {
		List<DailyCheck> lists = dailyCheckDao.findByBaseId(baseInfo);
		StringBuffer buffer = new StringBuffer();

		buffer.append("<div id=\"new_record_list\">");
		for (String key : DailyCheck.items.keySet()) {
			buffer.append(record(key, DailyCheck.items.get(key), lists));
		}
		buffer.append("</div>");
		return buffer.toString();
	}

	public String record(String item, String title, List<DailyCheck> lists) {
		StringBuffer buffer = new StringBuffer();
		buffer.append("<div class=\"col-sm-6\"><h5>" + title + "</h5><div class=\"record_data\">");
		Boolean flag = false;

		// 有检查方式的检查项
		List<String> items = new ArrayList();
		items.add("blood_sugar");
		items.add("blood_oxygen");
		items.add("lung_function");
		items.add("fat_rate");

		for (int i = 0; i < lists.size(); i++) {
			DailyCheck dailyCheck = lists.get(i);
			if (item.equals(dailyCheck.getItem())) {
				flag = true;
				buffer.append("<p>检测时间：" + dailyCheck.getCheckDate());
				if (items.contains(item)){
					buffer.append("&nbsp;&nbsp;检测方式：" + dailyCheck.getType());
				}
				if (item.equals("blood_pressure")) {
					buffer.append("&nbsp;&nbsp;高压:" + dailyCheck.getCheckRes() + "&nbsp;低压:"+ dailyCheck.getLowPress());
				} else if(item.equals("height_weight")){
					buffer.append("&nbsp;&nbsp;身高:"+dailyCheck.getCheckRes()+"&nbsp;体重:"+dailyCheck.getWeight());
				}else if(item.equals("vital")){
					buffer.append("&nbsp;&nbsp;臀围:"+dailyCheck.getCheckRes()+"&nbsp;腰围:"+dailyCheck.getWaistline()+"&nbsp;胸围:"+dailyCheck.getChestval());
				}else{
					buffer.append("&nbsp;&nbsp;检测结果：" + dailyCheck.getCheckRes());
				}
			}
		}
		if (!flag) {
			buffer.append("<p class=\"empty_tip\"><br><br>无数据…</p>");
		}
		buffer.append("</div></div>");
		return buffer.toString();
	}

    /***
     *一个月的最新值
     * @param dailyCheck
     * @return
     */
    public DailyCheck findMonthLastValueByBaseId(DailyCheck dailyCheck){
        return dao.findMonthLastValueByBaseId(dailyCheck);
    }

    /**
     * 根据日常检查项目和userid查找对应记录
     * @param
     * @return
     */
    public Page<DailyCheck> findDailyCheckList(Page<DailyCheck> page, Map map){
		page.setPageNo(Integer.parseInt((String) map.get("pageNo")));
		if(StringUtils.isNotBlank((String) map.get("pageSize"))){
			page.setPageSize(Integer.parseInt((String) map.get("pageSize")));
		}else{
			page.setPageSize(10);
		}
		DailyCheck dailyCheck = new DailyCheck();
		dailyCheck.setItem((String)map.get("item"));
		dailyCheck.setBaseInfo((String)map.get("userId"));
		dailyCheck.setPage(page);
		page.setList(this.dao.findDailyCheckList(dailyCheck));
        return page;
    }

}